Imports System.Web.UI.WebControls

Namespace HBBSoft.Pivot
    Friend NotInheritable Class DisplayFilter
        Dim _ept As ePivotTableControl

        Public Sub New(ByRef ept As ePivotTableControl)
            _ept = ept
        End Sub

        Public Function ToTable() As Table
            Dim t As New Table
            Dim objDim As Dimension

            t.Rows.Add(New TableRow)
            t.Rows(t.Rows.Count - 1).Cells.Add(New TableCell)

            For Each objDim In _ept.PivotTable.FilterDimensions
                t.Rows(t.Rows.Count - 1).Cells(0).Controls.Add(GetFilterDimensionControl(objDim))
                t.Rows(t.Rows.Count - 1).Cells(0).Controls.Add(GetSpaceControl())
            Next

            Return t
        End Function

        Private Function GetFilterDimensionControl(ByRef objDim As Dimension) As LinkButton
            Dim lbtn As New LinkButton
            Dim strSelMember As String
            Dim strToolTip As String = ""

            If objDim.FilterMembers.Count = 0 Then
                strSelMember = "-"
            ElseIf objDim.FilterMembers.Count = 1 Then
                strSelMember = objDim.FilterMembers(0).Caption
            Else
                strSelMember = "..."

                Dim objmem As Member
                For Each objmem In objDim.FilterMembers
                    strToolTip &= objmem.Caption & ","
                Next
                strToolTip = strToolTip.Substring(0, strToolTip.Length - ",".Length)
            End If

            lbtn.Text = objDim.Caption & "[" & strSelMember & "]"
            lbtn.ToolTip = strToolTip
            lbtn.CssClass = _ept.FilterLinkCssClass
            lbtn.Attributes.Add("DimensionID", objDim.ID)
            AddHandler lbtn.Click, AddressOf _ept.FilterLinkButton_Click

            Return lbtn
        End Function

        Private Function GetSpaceControl() As Literal
            Dim lt As New Literal
            lt.Text = "&nbsp;&nbsp;&nbsp;&nbsp;"
            Return lt
        End Function

    End Class
End Namespace